/**
 * 积分商城-我的积分
 */
'use strict';
import React, {
    Component
} from 'react'
import {
    Link
} from 'react-router'
import Header from './Common/Header'
import LoadingBox from './Common/LoadingBox'
import {
    HttpService,
    dateFormat,
    Tips,
    pullDownRefresh,
    sxtUrl,
    jxtUrl
} from '../Tools'

import comment from '../Img/mall/task/comment.png'
import phone from '../Img/mall/task/tel.png'
import task from '../Img/mall/task/task.png'
import photo from '../Img/mall/task/photo.png'
import login from '../Img/mall/task/login.png'
import notice from '../Img/mall/task/notice.png'
import head from '../Img/mall/task/head.png'
import info from '../Img/mall/task/info.png'
import infoshare from '../Img/mall/task/infoshare.png'
import addbaby from '../Img/mall/task/addbaby.png'

export default class IntegralMyself extends Component {
    constructor(props) {
        super(props);

        this.page = 1;
        this.pagesize = 10;
        this.toBottom = 60;
        this.state = {
            remainRyz: 0,
            currentRyz: 0,
            dataList: [],
            dataLoad: 1,
            dataEnd: 0,
            dataNull: 0,
            integral: 0
        }

    }

    componentWillMount() {
        this.getIntegral();
    }

    componentWillUnmount() {
        window.onscroll = null
    }

    componentDidMount() {
        this.getData()
        //加载更多
        window.onscroll = () => {
            const {
                dataEnd,
                dataNull,
                page
            } = this.state;
            if (this.dataLoad == 1 || dataEnd == 1 || dataNull == 1)
                return;
            var scrollTop = 0;
            if (document.documentElement && document.documentElement.scrollTop) {
                scrollTop = document.documentElement.scrollTop;
            } else if (document.body) {
                scrollTop = document.body.scrollTop;
            }
            if (document.body.clientHeight - window.innerHeight - scrollTop <= this.toBottom) {
                this.page++;
                this.getData();
            }
        }
        const obj = document.getElementById('refreshbox')
        const contentObj = document.getElementById('pdownrefresh')
        const statusObj = document.getElementById('status')
        let rct = this;
        pullDownRefresh(obj, contentObj, statusObj, 100, function(data) {
            if (data.status == 'end' && data.top >= 100 - 20) {
                this.page = 1;
                this.getData(1);
                this.getIntegral()
            }
        }.bind(this))
    }

    //查询积分
    getIntegral() {
        HttpService.query({
            url: `/${sessionStorage.userrole=='家长'?'guardian':'teacherapp'}/credit/getCurrentCredits`,
            data: {
                token: sessionStorage.appToken,
                type: 0
            },
            success: (res => {
                if (res.code == 20014) {
                    this.setState({
                        remainRyz: res.data.overplusCredit,
                        currentRyz: res.data.currentCredit
                    })
                    return
                }
                Tips(res.data.msg)
            })
        })
    }

    //获取数据列表
    getData(status) {
        this.dataLoad = 1
        this.setState({
            dataLoad: 1,
            dataEnd: 0,
            dataNull: 0
        });
        if (status == 1)
            this.page = 1
        HttpService.query({
            // url: 'src/Data/integral/IntegralMyself.json',
            url: `/${sessionStorage.userrole=='家长'?'guardian':'teacherapp'}/credit/getCreditRecordPage`,
            data: {
                token: sessionStorage.appToken,
                pageNumber: this.page,
                pageSize: this.pagesize
            },
            success: (res => {
                this.dataLoad = 0
                this.setState({
                    dataLoad: 0
                });
                if (res.code == 20014) {
                    //获取数据成功
                    let dataList = res.data.result
                    if (status != 1)
                        dataList = this.state.dataList.concat(res.data.result);
                    this.setState({
                        dataList
                    });
                    if (res.data.totalPages == this.page) {
                        this.setState({
                            dataEnd: 1
                        });
                    } else if (res.data.result.length < this.pagesize) {
                        this.setState({
                            dataEnd: 1
                        });
                        if (dataList.length == 0) {
                            this.setState({
                                dataEnd: 0,
                                dataNull: 1
                            });
                        }
                    }
                    return;
                }
                Tips(res.msg);
            })
        })
    }

    //显示图标
    showIcon(ryType) {
        switch (ryType) {
            case '发布作业':
            case '回复作业':
                return task
            case '关爱学生':
                return phone
            case '发布照片':
            case '评价相册':
                return photo
            case '评价学生':
                return comment
            case '分享资讯':
                return infoshare
            case '浏览资讯':
                return info
            case '发布班级通知':
            case '查阅班级通知':
                return notice
            case '上传头像':
                return head
            case '添加孩子':
                return addbaby
            default:
                return login
        }
    }

    render() {
        const {
            dataLoad,
            dataEnd,
            dataNull,
            dataList,
            remainRyz,
            currentRyz
        } = this.state;
        let copy = sessionStorage.userrole == '教师' ? '荣誉值' : '积分';
        let dataLists = dataList.map(function(item) {
            const date = dateFormat(new Date(item.doTime), '-');
            const day = date.split(' ')[0].slice(5);
            const time = date.split(' ')[1];
            let icon = this.showIcon(item.name)
            return (
                <li className="list">
                    <span className="time">
                        <span className="day">{day}</span>
                        <span className="hour">{time}</span>
                    </span>
                    <img src={icon} className="img"/>
                    <span className="tilte">{item.name}</span>
                    <span className="number">{item.num}</span>
                </li>
            )
        }.bind(this))
        return (
            <div className="punkte_page">
                <Header title={'我的' + copy}/>
                <div id='refreshbox'>
                    <div id='status'><span></span></div>
                    <div id='pdownrefresh'>
                        <div className="punkte_header">
                            <span className="title">剩余{copy}</span>
                            <span className="number">{remainRyz}</span>
                            <span className="img"/>
                            <span className="annotation">{copy}可用于兑换商城商品</span>
                        </div>
                        <div className="get_punkte">
                            <div className="get_punkte_header">
                                <span className="line"/>
                                <span className="left_tilte">获得{copy}记录</span>
                                <span className="number">{currentRyz}</span>
                                <span className="right_tilte">本月{copy}&nbsp;</span>
                            </div>
                            <ul>
                                {dataLists}
                                <LoadingBox dataLoad={dataLoad} dataNull={dataNull} dataEnd={dataEnd}/>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        )
    }
}
